package example.demo.service.impl;

import com.google.common.collect.Lists;
import example.demo.datasouce.TargetDataSource;
import example.demo.entity.User;
import example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserService {
    @Resource
    UserMapper userMapper;
    @Autowired
    UserService userService;

    public List<User> users() {
        List<User> users = Lists.newArrayList();
        users.add(userService.userFromDB1());
        users.add(userService.userFromDB2());
        users.add(userService.userFromDB3());
        return users;
    }


    public User userFromDB1() {
        return userMapper.selectById(111);
    }

    @TargetDataSource(name = "db2")
    @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public User userFromDB2() {
        return userMapper.selectById(222);
    }

    @TargetDataSource(name = "db3")
    @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public User userFromDB3() {
        return userMapper.selectById(333);
    }
}
